package 多线程;

import java.util.LinkedList;

//wait()和notify（）实现生产者和消费者
public class ProAndCon {

    public static final int MAX_SIZE = 2;
    public static LinkedList<Integer> list = new LinkedList<>();

    class Producer implements Runnable {
        @Override
        public void run() {
            synchronized (list){
                while(list.size()==MAX_SIZE){
                    System.out.println("仓库已满，生产者"+Thread.currentThread().getName()+"不可生产");
                    try {
                        list.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                list.add(1);
                System.out.println("生产者"+Thread.currentThread().getName()+"生产，仓库容量为"+list.size());
                list.notify();
            }
        }
    }

    class Consumer implements Runnable {
        @Override
        public void run() {
            synchronized (list){
                while(list.size()==0){
                    System.out.println("仓库为空，消费者"+Thread.currentThread().getName()+"不可消费");
                }
                try {
                    list.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            list.removeFirst();
            System.out.println("消费者"+Thread.currentThread().getName()+"消费,仓库容量为"+list.size());
            list.notify();
        }
    }

    public static void main(String[] args) {
        ProAndCon proAndCon = new ProAndCon();
        Producer producer = proAndCon.new Producer();
        Consumer consumer = proAndCon.new Consumer();
        for (int i = 0; i < 10; i++) {
            Thread pro = new Thread(producer);
            pro.start();
            Thread con = new Thread(consumer);
            con.start();
        }
    }

}
